from rest_framework import serializers
from rest_framework.request import Request
from ac import models
from django.db import models as dbModles
import ars.forms as forms
import time


class ProjectSerializer(serializers.ModelSerializer):
    # article_comments_count =
    manager = serializers.ReadOnlyField(source='manager.username')
    proposer = serializers.ReadOnlyField(source='proposer.username')
    # proposer = serializers.HyperlinkedIdentityField(view_name='ars-user')       # view_name是需要跳轉的URL中的name參數
    # manager = UserSerializer()
    # proposer = UserSerializer()
    # serializers.DateTimeField()
    # print(super().instance)

    class Meta:
        model = models.Project
        # setattr(model, "modules", get_module(model.PID))
        fields = ('id', 'PID', 'short_name', 'full_name', 'desc', 'http_address', 'server_ip', 'create_date',
                  'manager', 'proposer', 'have_app')
        # fields = "__all__"
    # def get_article_comments_count(self):
    #     return self.instance

    # def get_module(self, obj):
    #     return obj.short_name + "qqq"


class CheckSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.MyUser
        fields = ('username', 'password')


class CustomDateTimeField(serializers.DateTimeField):
    def to_representation(self, value):
        if not value:
            return None
        value = time.timezone.localtime(value)
        value = int(value.timestamp())
        return value


def get_module(name):
    return name + "qqqqqq"


class RoleSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Role
        fields = "__all__"

#
# class RolesSerializer(serializers.ModelSerializer):
#     # id = serializers.HyperlinkedIdentityField(view_name="ars-role")
#
#     class Meta:
#         model = models.Role
#         fields = "__all__"


class FunctionSerializer(serializers.ModelSerializer):

    # project = serializers.ReadOnlyField(source="project.__str__")
    # definer = serializers.ReadOnlyField(source="definer.__str__")
    # project = ProjectSerializer()

    class Meta:
        model = models.Function
        fields = ('id', 'code', 'name', 'parent_id', 'parent_code', 'full_code', 'desc', 'desc2', 'create_date',
                  'is_active', 'project', 'definer')


class FunctionsSerializer(serializers.ModelSerializer):

    # id = serializers.HyperlinkedIdentityField(view_name='ars-function')
    # parent_id = FunctionSerializer()

    class Meta:
        model = models.Function

        fields = "__all__"

    # def get_models(self):
    #     for i in self.instance:
    #         print(i)
    # def get_initial(self):
    #     return super(FunctionsSerializer, self).instance


class ChildFunctionSerializer(serializers.ModelSerializer):
    project = serializers.ReadOnlyField(source="project.short_name")

    class Meta:
        model = models.Function
        fields = "__all__"


class UT(serializers.ModelSerializer):
    """
    仅显示用户表的单独几个字段，特殊序列化使用
    """
    id = serializers.IntegerField(read_only=False)

    class Meta:
        model = models.MyUser
        fields = ("id", "username", "last_name")
        read_only_fields = ("username", "last_name")


class RT(serializers.ModelSerializer):
    """
    仅显示Role表的几个字段，特殊序列化需求使用
    """
    id = serializers.CharField(read_only=False)

    class Meta:
        model = models.Role
        fields = ("id", "role_name")
        read_only_fields = ("role_name", )


# class UserRoleMapsSerializer(serializers.ModelSerializer):
#     user = UT()
#     role = RT()
#
#     class Meta:
#         model = models.UserRoleMap
#         # fields = ("user", "role")
#         fields = "__all__"


class UserRoleMapSerializer(serializers.ModelSerializer):

    user = UT()
    role = RT()

    class Meta:
        model = models.UserRoleMap
        # fields = ("user", "role")
        fields = "__all__"


# class ProjectRoleSerializer(serializers.ModelSerializer):
#
#     class Meta:
#         model = models.Role
#         fields = "__all__"


class RoleFunctionSerializer(serializers.ModelSerializer):
    role = RoleSerializer()

    class Meta:
        model = models.RoleFunctionMap
        fields = "__all__"


class BulkAddFunctionSerializer(serializers.ModelSerializer):
    functions = serializers.CharField(help_text="請輸入功能ID的列表")
    roles = serializers.CharField(help_text="請輸入角色ID的列表")

    class Meta:
        model = forms.PublicForm
        fields = "__all__"
